import baseUrl from '../utils/baseUrlConf.js'
// const baseUrl = 'http://106.13.139.181:6767/'
// const baseUrl = 'http://192.168.0.104/'


 function addHeader(){
	return {'token': uni.getStorageSync('token')}	
}

const getData = {
  method: 'GET'
}

const postData = {
  method: 'POST',
  header: {
    "content-type": "application/json"
  }
}

const postFormData = {
  method: 'POST',
  header: {
    "content-type": "application/x-www-form-urlencoded"
  }
}

const postMultipartData = {
  method: 'POST',
  header: {
    "content-type": "multipart/form-data"
  }
}


// promise  get 统一返回的函数
const getPromise = (requestData,loding=false) => {
	if(loding){
		uni.showLoading({
			title: '加载中',
			mask: true
		})
	}
  return new Promise((resolve, reject) => {
    uni.request(Object.assign(requestData, {
		header: addHeader(),
		success: (res) => {
			if (res.data.code==200) {
			  resolve(res.data)
			}else if(res.data.code==300){
				// uni.removeStorageSync('token')
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}else{
				// console.log('err msg:',res.code)
				reject(res.data.msg)
				
			}
			
		  },
		complete: () => {
			  if(loding){
				uni.hideLoading()
			  } 
		}
    }))
  })
}

/*登录接口*/
const apiLogin = (phone,password) => {
	let datasrc = {
		phone,password
	}
	// if(password){
	// 	datasrc.password = password
	// }
	// if(code){
	// 	datasrc.code = code
	// }
	let requestData = {
		url: baseUrl+ 'v1/user/login',
		method: 'POST',
		data: datasrc
	}
	return getPromise(requestData,true)
}
/*发送验证码*/
const sendCode = (phone,type = '') => {
  let requestData = {
    url: baseUrl+ 'v1/user/sendPhone?phone='+phone + '&type=' + type
  }
  return getPromise(requestData)
}
/*用户注册*/
const userRegister = (phone,code,password,inviteCode) => {
  let requestData = {
    url: baseUrl+ 'v1/user/register',
	method: 'POST',
	data: {
		phone,code,password,inviteCode
	}
  }
  return getPromise(requestData)
}
/*找回密码*/
const forgetPwd = (phone,code,password) => {
  let requestData = {
    url: baseUrl+ 'v1/user/reset',
	method: 'POST',
	data: {
		phone,code,password
	}
  }
  return getPromise(requestData)
}
/*用户信息*/
const userInfo = (loading=true) => {
  let requestData = {
    url: baseUrl+ 'v1/user/getDetail'
  }
  return getPromise(requestData,loading)
}
/*领取任务*/
const getTask = (id,type) => {
  let requestData = {
    url: baseUrl+ 'v1/user/task/receive?taskId='+id+'&taskType='+type
  }
  return getPromise(requestData,true)
}
/**/
const taskCount = (id) => {
  let requestData = {
    url: baseUrl+ 'v1/user/task/count?taskId='+id
  }
  return getPromise(requestData,true)
}
/**/
const cancelTask = (id) => {
  let requestData = {
    url: baseUrl+ '/v1/user/task/cancel?did='+id
  }
  return getPromise(requestData)
}

/*绑定任务使用id*/
const bindTaskid = (taskId,urls,modal=false) => {
  let requestData = {
    url: baseUrl+ 'v1/user/bindUser',
	method: 'POST',
	data: {
		taskId,
		urls
	}
  }
  return getPromise(requestData,modal)
}
/*绑定支付宝*/
const bindalipay = (user,name) => {
  let requestData = {
    url: baseUrl+ 'v1/user/alipay?user='+user+'&name='+name
  }
  return getPromise(requestData,true)
}
/*用户提现*/
const outAmount = (number) => {
  let requestData = {
    url: baseUrl+ 'v1/user/outAmount',
	method: 'POST',
	data: {
		number
	}
  }
  return getPromise(requestData,true)
}
/*提现记录*/
const amountOutList = (page,pageSize) => {
  let requestData = {
    url: baseUrl+ 'v1/user/outList?page='+page+'&pageSize='+pageSize
  }
  return getPromise(requestData,true)
}

/*常见问题*/
const problemList = () => {
  let requestData = {
    url: baseUrl+ 'v1/user/problemList'
  }
  return getPromise(requestData,true)
}

/*任务记录*/
const taskRecord = (page,pageSize,taskId) => {
  let requestData = {
    url: baseUrl+ 'v1/user/task/record?page='+page+'&pageSize='+pageSize+'&taskId='+taskId
  }
  return getPromise(requestData,true)
}
/*问题反馈*/
const getFeedback = () => {
  let requestData = {
    url: baseUrl+ 'v1/user/getFeedback'
  }
  return getPromise(requestData,true)
}
/*任务类型列表*/
const taskTypeList = (page,pageSize) => {
  let requestData = {
    url: baseUrl+ 'v1/user/task/lists'
  }
  return getPromise(requestData,true)
}
/*绑定账号记录*/
const accountList = (id) => {
  let requestData = {
    url: baseUrl+ 'v1/user/getUserList?taskId='+id
  }
  return getPromise(requestData,true)
}
/*删除历史绑定账号*/
const deleteAccount = (id) => {
  let requestData = {
    url: baseUrl+ 'v1/user/delete?id='+id
  }
  return getPromise(requestData)
}
/*代理明细*/
const agentDetailList = (page,pageSize) => {
  let requestData = {
    url: baseUrl+ 'v1/user/agent/detail?page='+page+'&pageSize='+pageSize
  }
  return getPromise(requestData,true)
}
/*收入明细*/
const integralDetail = (page,pageSize) => {
  let requestData = {
    url: baseUrl+ 'v1/user/integral/lists?page='+page+'&pageSize='+pageSize
  }
  return getPromise(requestData,true)
}
/*签到列表*/
const signInList = () => {
  let requestData = {
    url: baseUrl+ 'v1/user/signIn/lists'
  }
  return getPromise(requestData,true)
}
/*用户签到*/
const signInAdd = () => {
  let requestData = {
    url: baseUrl+ 'v1/user/signIn/add'
  }
  return getPromise(requestData,true)
}

/*公告列表*/
const noticeList = (page,pageSize) => {
  let requestData = {
    url: baseUrl+ 'v1/user/notice/lists?page='+page+'&pageSize='+pageSize
  }
  return getPromise(requestData,true)
}
/*公告详情*/
const noticeDetail = (noticeId) => {
  let requestData = {
    url: baseUrl+ 'v1/user/notice/detail?noticeId='+noticeId
  }
  return getPromise(requestData,true)
}
/*app版本信息*/
const initApp = () => {
  let requestData = {
    url: baseUrl+ 'v1/initApp'
  }
  return getPromise(requestData)
}
/*获取教程*/
const tutorial = (id) => {
  let requestData = {
    url: baseUrl+ 'v1/user/task/tutorial?taskId='+id
  }
  return getPromise(requestData,true)
}
module.exports = {
	apiLogin,
	sendCode,
	userInfo,
	getTask,
	taskCount,
	cancelTask,
	bindTaskid,
	bindalipay,
	outAmount,
	amountOutList,
	problemList,
	taskRecord,
	getFeedback,
	taskTypeList,
	accountList,
	deleteAccount,
	agentDetailList,
	integralDetail,
	signInList,
	signInAdd,
	noticeList,
	noticeDetail,
	initApp,
	tutorial,
	userRegister,
	forgetPwd
}